package com.trkj.service.hjj;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.trkj.dao.hjj.DeptDAO;
import com.trkj.dao.hjj.GerenLxlDAO;
import com.trkj.dao.hjj.PaibanplanDAO;
import com.trkj.dao.hjj.PbzhouqiDAO;
import com.trkj.dao.hjj.SchedulingDAO;
import com.trkj.dao.hjj.TeamDAO;
import com.trkj.dao.wdm.StaffInformationDAO;
import com.trkj.mybatisdao.PbzhouqiDAOI;
import com.trkj.mybatisdao.PersonalDAOI;
import com.trkj.mybatisdao.StaffinformationDAOI;
import com.trkj.mybatisdao.TeamDAOI;
import com.trkj.pojo.Confirmaffair;
import com.trkj.pojo.DefinitionDept;
import com.trkj.pojo.GerenLxl;
import com.trkj.pojo.Paibanplan;
import com.trkj.pojo.Pbzhouqi;
import com.trkj.pojo.Scheduling;
import com.trkj.pojo.Shift;
import com.trkj.pojo.StaffInformation;
import com.trkj.pojo.Team;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor=Exception.class)
public class SchedulingService {
	@Autowired
	private TeamDAOI team;
	@Autowired
	private TeamDAO teamdao;
	@Autowired
	private PersonalDAOI personalDAOI;
	@Autowired
	private DeptDAO deptdao;
	@Autowired
	private SchedulingDAO schedulingDAO;
	@Autowired
	private StaffinformationDAOI staffinformationDAOI;
	@Autowired
	private StaffInformationDAO staffinformationDAO;
	/*GerenLxlDAO*/
	@Autowired
	private GerenLxlDAO gerenLxlDAO;
	@Autowired
	private PbzhouqiDAOI pbzhouqiDAOI;
	@Autowired
	private PbzhouqiDAO pbzhouqiDAO;
	@Autowired
	private PaibanplanDAO paibanplanDAO;

	
	//排版中心清单查找
	public JSONArray findallspaiban(){
		return pbzhouqiDAOI.findpaiban();
	}
	
	//排版中心清单查找 模糊搜索
		public JSONArray findallspaibanbyName(String teamName){
			return pbzhouqiDAOI.findpaibanbyName("%"+teamName+"%");
		}
		//排版中心清单查找 精确搜索
	public JSONArray findallspaibanbyNamejq(String teamName){
		return pbzhouqiDAOI.findpaibanbyNamejq(teamName);
	}
	//排班第二个界面
	public List<Scheduling> findzhouqi(Long scheduId){
		return schedulingDAO.pazhouqi(scheduId);
	}
	//排班返回第一步
	public List<Team> findzhouqis(Long teamId){
		return  (List<Team>) teamdao.findOne(teamId);
	}
	public List<Scheduling> findzhouqione(Long scheduId){
		return (List<Scheduling>) schedulingDAO.findOne(scheduId);
	}
	//班组查询所有
	public JSONArray findalls(){
		return team.findisall();
	}
	
	public JSONArray findisallbyName(String teamName){
		return team.findisallbyName("%"+teamName+"%");
	}
	//班组查询名称编号
	public JSONArray findallsone(){
		return team.findallsone();
	}
	//班组查询名称编号模糊
	public JSONArray findallsonemh(String teamName){
		return team.findallsonemh("%"+teamName+"%");
	}

	/**
	 * 根据员工的姓名进行查询
	 * @param key
	 * @return
	 */
	public JSONArray queryByName(String key){
		return team.findallselect("%"+key+"%");
	}
	public List<Team> finteam(){
		return (List<Team>) teamdao.findAll();
	}
	
	//部门表查询所有
	public JSONArray deptfindall(){
		return team.findalldept();
	}
	//个人调班查询所有
	public JSONArray findallsper(){
		return personalDAOI.findpersonal();
	}
	//个人调班查
	public JSONArray findpersonalbyid(Integer key){
		return personalDAOI.findpersonalbyid(key);
	}
	
	//排班模糊查询查询所有
	public JSONArray squerysByName(String namees){
		return  staffinformationDAOI.findAllpaibanteam("%"+namees+"%");
		
	}
	//排班模糊查询
	public JSONArray findallsteam(String namees){
		return staffinformationDAOI.findAllteama("%"+namees+"%");
	}
	//员工名称模糊查询
	public JSONArray findallsteamyg(String namees){
		return staffinformationDAOI.findAllempname("%"+namees+"%");
	}
	//班组名称模糊查询
	public JSONArray findallsteambz(String namees){
		return staffinformationDAOI.findAllteamname("%"+namees+"%");
	}
	
	//员工表查询所有
	public JSONArray empfindall(){
		return staffinformationDAOI.findstaffinfor();
	}
	//排版安排
	public void hhh(Long planId,Long teamId,Long scheduId){
		Team team=teamdao.findOne(teamId);
		Scheduling sch=schedulingDAO.findOne(scheduId);
		Paibanplan paiban=new Paibanplan();
		paiban.setTeam(team);
		paiban.setScheduling(sch);
		paibanplanDAO.save(paiban);
	}
	/**
	 * 班组新增
	 * @param option
	 */
	//definition_dept
	public void teamadds(Long teamId,String teamName,Timestamp teamTakeeffect,Timestamp teamLnvalid,
		String teamRemarks ,Timestamp teamFounddate,Long defideId,String defideName,String stainName){
		DefinitionDept deptfin=deptdao.findOne(defideId);
		Team te =  new Team();
		te.setDefinitionDept(deptfin);
		te.setTeamName(teamName);
		te.setTeamTakeeffect(teamTakeeffect);
		te.setTeamFounddate(new Timestamp(new Date().getTime()));
		te.setTeamLnvalid(teamLnvalid);
		te.setTeamRemarks(teamRemarks);
		te.setTeamPerson(stainName);
		teamdao.save(te);
	} 
	/**
	 * 编辑
	 * @param leavo
	 */
	public void teamupdates(Long teamId,String teamName,Timestamp teamTakeeffect,Timestamp teamLnvalid,
			String teamRemarks ,Timestamp teamFounddate,/*String 	teamPerson,Integer team_state,*/Long 	defideId,String defideName){
			DefinitionDept deptfin=deptdao.findOne(defideId);
			/*deptfin.setDefideName(defideName);*/
			Team te = teamdao.findOne(teamId);
			te.setDefinitionDept(deptfin);
			te.setTeamName(teamName);
			te.setTeamTakeeffect(teamTakeeffect);
			te.setTeamUpdate(new Timestamp(new Date().getTime()));
			te.setTeamLnvalid(teamLnvalid);
			te.setTeamRemarks(teamRemarks);
			teamdao.save(te);

		} 
	/**
	 * 删除
	 * @param 
	 * @return
	 */
	public int Salesdele(Long teamId){
		try {
		Team tea=teamdao.findOne(teamId);
		if(tea.getGerenLxls().isEmpty()){
			teamdao.delete(teamId);
			return 1;
		}else{
			return 0;
		}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	//排版模板
	/**
	 * 排版模板
	 */
	//排版模板
	public JSONArray schedulingtempl(){
		return pbzhouqiDAOI.findpaiban();
		
	}
	/**
	 * 获取总记录条数
	 * @return
	 */
	public long attByPageCount() {
		return schedulingDAO.count();
	}
	/**
	 * 排班新增
	 * @param option
	 */
	//definition_dept
	public void teamapldds(Long scheduId,String scheduName,BigDecimal cycle,BigDecimal scheduTeam,BigDecimal scheduCurrentcycle,BigDecimal scheduCurrentteam,String scheduRemarks,String scheduPerson,BigDecimal scheduState,Timestamp scheduEnabledate){
		Scheduling te =  new Scheduling();
		te.setScheduName(scheduName);
		te.setCycle(cycle);
		te.setScheduTeam(scheduTeam);
		te.setScheduCurrentcycle(scheduCurrentcycle);
		te.setScheduCurrentteam(scheduCurrentteam);
		te.setScheduPerson(scheduPerson);
		te.setScheduRemarks(scheduRemarks);
		te.setScheduState(scheduState);
		te.setScheduEnabledate(scheduEnabledate);
		schedulingDAO.save(te);
		System.out.println(scheduEnabledate);
	} 
	/**
	 * 删除
	 * @param 
	 * @return
	 */
	public boolean Salesdelee(Long scheduId){
		Scheduling sch=schedulingDAO.findOne(scheduId);
		if(sch.getPaibanplans().isEmpty()){
			schedulingDAO.delete(scheduId);
			return true;
		}else{
			return false;
		}
		
	}
	/**
	 * 编辑
	 * @param scheduId
	 * @return
	 */
	public Scheduling deitff(Long scheduId){
		Scheduling attr =schedulingDAO.findOne(scheduId);
		System.out.println(attr);
		return attr;
	}
	/**
	 * 修改
	 * @param scheduId
	 * @param scheduName
	 * @param scheduEnabledate
	 * @param scheduRemarks
	 */
	public void updateaffair(Long scheduId,String scheduName,Timestamp scheduEnabledate,String scheduRemarks) {
		Scheduling attr =schedulingDAO.findOne(scheduId);
		attr.setScheduName(scheduName);
		attr.setScheduEnabledate(scheduEnabledate);
		attr.setScheduRemarks(scheduRemarks);
		schedulingDAO.save(attr);
	}
	//
	/**
	 * 个人调班新增
	 * @param option
	 */
	//definition_dept
	public void teamaresdds(Long gerenId,Long teamId,String teamName,Long stainId,String stainName){
		GerenLxl ger=new GerenLxl();//个人调班新id
		StaffInformation deptfin=staffinformationDAO.findOne(stainId);
		Team te =  teamdao.findOne(teamId);
		ger.setStaffInformation(deptfin);
		ger.setTeam(te);
		gerenLxlDAO.save(ger);
	} 
	/**
	 * 个人调班编辑
	 * @param option
	 */
	//definition_dept
	public void personupdates(Long gerenId,Long teamId,String teamName,Long stainId,String stainName){
		GerenLxl ger=gerenLxlDAO.findOne(gerenId);//个人调班id
		StaffInformation deptfin=staffinformationDAO.findOne(stainId);
		Team te =  teamdao.findOne(teamId);
		ger.setStaffInformation(deptfin);
		ger.setTeam(te);
		gerenLxlDAO.save(ger);
	}
	/**
	 * 删除
	 * @param 
	 * @return
	 */
	public void Salesdeleeper(Long peruId){
		gerenLxlDAO.delete(peruId);
	}
	
}
